cmake_minimum_required(VERSION 3.5)
set(TARGET_NAME MkCore)
project(${PROJECT_NAME})

if(NOT CMAKE_INSTALL_PREFIX)
if(MSVC)
    set(CMAKE_INSTALL_PREFIX "/usr")
else(MSVC)
    set(CMAKE_INSTALL_PREFIX "C:/Program Files (x86)")
endif(MSVC)
endif()

set(INSTALL_LIB_DIR ${CMAKE_INSTALL_PREFIX}/lib)
set(INSTALL_INC_DIR ${CMAKE_INSTALL_PREFIX}/include)
set(INSTALL_PKGCONFIG_DIR ${INSTALL_LIB_DIR}/pkgconfig)

file(GLOB UTIL_SOURCES CONFIGURE_DEPENDS ${CMAKE_CURRENT_LIST_DIR}/MkUtil/*.cpp)
file(GLOB UTIL_HEADERS CONFIGURE_DEPENDS  ${CMAKE_CURRENT_LIST_DIR}/MkUtil/*.h)
source_group("Util"  FILES ${UTIL_SOURCES} ${UTIL_HEADERS})
list(APPEND SOURCES ${UTIL_SOURCES} ${UTIL_HEADERS})
install(FILES ${UTIL_HEADERS} DESTINATION ${INSTALL_INC_DIR}/MkUtil)

file(GLOB GB28181_HEADER ${CMAKE_CURRENT_LIST_DIR}/MkGb28181/*.h)
file(GLOB GB28181_SOURCE ${CMAKE_CURRENT_LIST_DIR}/MkGb28181/*.cpp)
source_group("Gb28181"  FILES ${GB28181_HEADER} ${GB28181_SOURCE})
list(APPEND SOURCES ${GB28181_HEADER} ${GB28181_SOURCE})
install(FILES ${GB28181_HEADER} DESTINATION ${INSTALL_INC_DIR}/MkGb28181)

file(GLOB STATE_GRID_A_HEADER ${CMAKE_CURRENT_LIST_DIR}/MkStateGridA/*.h)
file(GLOB STATE_GRID_A_SOURCE ${CMAKE_CURRENT_LIST_DIR}/MkStateGridA/*.cpp)
list(APPEND SOURCES ${STATE_GRID_A_HEADER} ${STATE_GRID_A_SOURCE})
source_group("StateGridA"  FILES ${STATE_GRID_A_HEADER} ${STATE_GRID_A_SOURCE})
install(FILES ${STATE_GRID_A_HEADER} DESTINATION ${INSTALL_INC_DIR}/MkStateGridA)

file(GLOB STATE_GRID_B_HEADER ${CMAKE_CURRENT_LIST_DIR}/MkStateGridB/*.h)
file(GLOB STATE_GRID_B_SOURCE ${CMAKE_CURRENT_LIST_DIR}/MkStateGridB/*.cpp)
source_group("StateGridB"  FILES ${STATE_GRID_B_HEADER} ${STATE_GRID_B_SOURCE})
list(APPEND SOURCES ${STATE_GRID_B_HEADER} ${STATE_GRID_B_SOURCE})
install(FILES ${STATE_GRID_B_HEADER} DESTINATION ${INSTALL_INC_DIR}/MkStateGridB)

file(GLOB SDP_HEADER ${CMAKE_CURRENT_LIST_DIR}/MkSdp/*.h)
file(GLOB SDP_SOURCE ${CMAKE_CURRENT_LIST_DIR}/MkSdp/*.cpp)
source_group("Sdp"  FILES ${SDP_HEADER} ${SDP_SOURCE})
list(APPEND SOURCES ${SDP_HEADER} ${SDP_SOURCE})
install(FILES ${SDP_HEADER} DESTINATION ${INSTALL_INC_DIR}/MkSdp)

file(GLOB SIP_HEADER ${CMAKE_CURRENT_LIST_DIR}/MkSip/*.h)
file(GLOB SIP_SOURCE ${CMAKE_CURRENT_LIST_DIR}/MkSip/*.cpp)
source_group("Sip"  FILES ${SIP_HEADER} ${SIP_SOURCE})
list(APPEND SOURCES ${SIP_HEADER} ${SIP_SOURCE})
install(FILES ${SIP_HEADER} DESTINATION ${INSTALL_INC_DIR}/MkSip)


file(GLOB RTMP_SOURCE ${CMAKE_CURRENT_LIST_DIR}/MkRtmp/*.cpp)
file(GLOB RTMP_HEADER ${CMAKE_CURRENT_LIST_DIR}/MkRtmp/*.h)
source_group("Rtmp"  FILES ${RTMP_SOURCE} ${RTMP_HEADER})
list(APPEND SOURCES ${RTMP_SOURCE} ${RTMP_HEADER})
install(FILES ${RTMP_HEADER} DESTINATION ${INSTALL_INC_DIR}/MkRtmp)

file(GLOB FLV_SOURCE ${CMAKE_CURRENT_LIST_DIR}/MkFlv/*.cpp)
file(GLOB FLV_HEADER ${CMAKE_CURRENT_LIST_DIR}/MkFlv/*.h)
file(GLOB AMF_SOURCE ${CMAKE_CURRENT_LIST_DIR}/MkAmf/*.cpp)
file(GLOB AMF_HEADER ${CMAKE_CURRENT_LIST_DIR}/MkAmf/*.h)	
source_group("Amf"  FILES ${AMF_SOURCE} ${AMF_HEADER})
source_group("Flv"  FILES ${FLV_SOURCE} ${FLV_HEADER})
list(APPEND SOURCES ${FLV_SOURCE} ${FLV_HEADER} ${AMF_SOURCE} ${AMF_HEADER})
install(FILES ${FLV_HEADER} DESTINATION ${INSTALL_INC_DIR}/MkFlv)
install(FILES ${AMF_HEADER} DESTINATION ${INSTALL_INC_DIR}/MkAmf)

file(GLOB PS_SOURCE ${CMAKE_CURRENT_LIST_DIR}/MkPs/*.cpp)
file(GLOB PS_HEADER ${CMAKE_CURRENT_LIST_DIR}/MkPs/*.h)
source_group("Ps"  FILES ${PS_SOURCE} ${PS_HEADER})
list(APPEND SOURCES ${PS_SOURCE} ${PS_HEADER} )
install(FILES ${PS_HEADER} DESTINATION ${INSTALL_INC_DIR}/MkPs)

file(GLOB MEDIA_SOURCE ${CMAKE_CURRENT_LIST_DIR}/MkMedia/*.cpp)
file(GLOB MEDIA_HEADER ${CMAKE_CURRENT_LIST_DIR}/MkMedia/*.h)
source_group("Media"  FILES ${MEDIA_SOURCE} ${MEDIA_HEADER})
list(APPEND SOURCES ${MEDIA_SOURCE} ${MEDIA_HEADER})
install(FILES ${MEDIA_HEADER} DESTINATION ${INSTALL_INC_DIR}/MkMedia)

file(GLOB GB_MEDIA_SOURCE ${CMAKE_CURRENT_LIST_DIR}/MkGbMedia/*.cpp)
file(GLOB GB_MEDIA_HEADER ${CMAKE_CURRENT_LIST_DIR}/MkGbMedia/*.h)
source_group("GbMedia"  FILES ${GB_MEDIA_SOURCE} ${GB_MEDIA_HEADER})
list(APPEND SOURCES ${GB_MEDIA_SOURCE} ${GB_MEDIA_HEADER})
install(FILES ${GB_MEDIA_HEADER} DESTINATION ${INSTALL_INC_DIR}/MkGbMedia)

file(GLOB RTSP_SOURCE ${CMAKE_CURRENT_LIST_DIR}/MkRtsp/*.cpp)
file(GLOB RTSP_HEADER ${CMAKE_CURRENT_LIST_DIR}/MkRtsp/*.h)
source_group("Rtsp"  FILES ${RTSP_SOURCE} ${RTSP_HEADER})
list(APPEND SOURCES ${RTSP_SOURCE} ${RTSP_HEADER})
install(FILES ${RTSP_HEADER} DESTINATION ${INSTALL_INC_DIR}/MkRtsp)

file(GLOB RTP_HEADER ${CMAKE_CURRENT_LIST_DIR}/MkRtp/*.h)
file(GLOB RTP_SOURCE ${CMAKE_CURRENT_LIST_DIR}/MkRtp/*.cpp)
source_group("Rtp"  FILES ${RTP_SOURCE} ${RTP_HEADER})
list(APPEND SOURCES ${RTP_HEADER} ${RTP_SOURCE})
install(FILES ${RTP_HEADER} DESTINATION ${INSTALL_INC_DIR}/MkRtp)

file(GLOB HTTP_MEDIA_SOURCE ${CMAKE_CURRENT_LIST_DIR}/MkHttpMedia/*.cpp)
file(GLOB HTTP_MEDIA_HEADER ${CMAKE_CURRENT_LIST_DIR}/MkHttpMedia/*.h)
source_group("HttpMedia"  FILES ${HTTP_MEDIA_HEADER} ${HTTP_MEDIA_SOURCE})
list(APPEND SOURCES ${HTTP_MEDIA_HEADER} ${HTTP_MEDIA_SOURCE})
install(FILES ${HTTP_MEDIA_HEADER} DESTINATION ${INSTALL_INC_DIR}/MkHttpMedia)

file(GLOB HTTP_HOOKER_SOURCE ${CMAKE_CURRENT_LIST_DIR}/MkHttpHooker/*.cpp)
file(GLOB HTTP_HOOKER_HEADER ${CMAKE_CURRENT_LIST_DIR}/MkHttpHooker/*.h)	
source_group("HttpHooker"  FILES ${HTTP_HOOKER_SOURCE} ${HTTP_HOOKER_HEADER})
list(APPEND SOURCES ${HTTP_HOOKER_SOURCE} ${HTTP_HOOKER_HEADER})
install(FILES ${HTTP_HOOKER_HEADER} DESTINATION ${INSTALL_INC_DIR}/MkHttpHooker)

file(GLOB HTTP_API_SOURCE ${CMAKE_CURRENT_LIST_DIR}/MkHttpApi/*.cpp)
file(GLOB HTTP_API_HEADER ${CMAKE_CURRENT_LIST_DIR}/MkHttpApi/*.h)
source_group("HttpApi"  FILES ${HTTP_API_SOURCE} ${HTTP_API_HEADER})
list(APPEND SOURCES ${HTTP_API_SOURCE} ${HTTP_API_HEADER})
install(FILES ${HTTP_API_HEADER} DESTINATION ${INSTALL_INC_DIR}/MkHttpApi)

file(GLOB HTTP_SOURCE ${CMAKE_CURRENT_LIST_DIR}/MkHttp/*.cpp)
file(GLOB HTTP_HEADER ${CMAKE_CURRENT_LIST_DIR}/MkHttp/*.h)
source_group("Http"  FILES ${HTTP_SOURCE} ${HTTP_HEADER})
list(APPEND SOURCES ${HTTP_SOURCE} ${HTTP_HEADER})
install(FILES ${HTTP_HEADER} DESTINATION ${INSTALL_INC_DIR}/MkHttp)

file(GLOB SOCKET_SOURCES ${CMAKE_CURRENT_LIST_DIR}/MkSocket/*.cpp)
file(GLOB SOCKET_HEADERS ${CMAKE_CURRENT_LIST_DIR}/MkSocket/*.h)
file(GLOB EVENT_SOURCES ${CMAKE_CURRENT_LIST_DIR}/MkEvent/*.cpp)
file(GLOB EVENT_HEADERS ${CMAKE_CURRENT_LIST_DIR}/MkEvent/*.h)
source_group("NetworkBase"  FILES ${SOCKET_SOURCES} ${SOCKET_HEADERS} ${EVENT_SOURCES} ${EVENT_HEADERS})
file(GLOB TCP_SOURCE ${CMAKE_CURRENT_LIST_DIR}/MkTcp/*.cpp)
file(GLOB TCP_HEADER ${CMAKE_CURRENT_LIST_DIR}/MkTcp/*.h)
source_group("Tcp"  FILES ${TCP_SOURCE} ${TCP_HEADER})
install(FILES ${TCP_HEADER} DESTINATION ${INSTALL_INC_DIR}/MkTcp)
install(FILES ${EVENT_HEADERS} DESTINATION ${INSTALL_INC_DIR}/MkEvent)
install(FILES ${SOCKET_HEADERS} DESTINATION ${INSTALL_INC_DIR}/MkSocket)
LIST(APPEND SOURCES ${SOCKET_SOURCES} ${SOCKET_HEADERS} ${EVENT_SOURCES} ${EVENT_HEADERS})
LIST(APPEND SOURCES ${TCP_SOURCE} ${TCP_HEADER})

include_directories(${CMAKE_CURRENT_BINARY_DIR})
include_directories(${CMAKE_CURRENT_LIST_DIR})

if(BUILD_SHARED)
    add_definitions(-DMkSharedBuild)
    set(MkCoreShareBuld "1")
	add_library(${TARGET_NAME}-shared SHARED ${SOURCES})
    set_target_properties(${TARGET_NAME}-shared PROPERTIES OUTPUT_NAME ${TARGET_NAME})
    set_target_properties(${TARGET_NAME}-shared PROPERTIES VERSION ${VERSION})
    set_target_properties(${TARGET_NAME}-shared PROPERTIES CLEAN_DIRECT_OUTPUT 1)
    target_link_libraries(${TARGET_NAME}-shared ${LIBS})
    install(TARGETS ${TARGET_NAME}-shared DESTINATION ${INSTALL_LIB_DIR})
else()
    set(MkCoreShareBuld "0")
    add_library(${TARGET_NAME}-static STATIC ${SOURCES})
    set_target_properties(${TARGET_NAME}-static PROPERTIES OUTPUT_NAME ${TARGET_NAME})
    set_target_properties(${TARGET_NAME}-static PROPERTIES VERSION ${VERSION})
    set_target_properties(${TARGET_NAME}-static PROPERTIES CLEAN_DIRECT_OUTPUT 1)
    install(TARGETS ${TARGET_NAME}-static DESTINATION ${INSTALL_LIB_DIR})
endif()

configure_file(${PROJECT_SOURCE_DIR}/${TARGET_NAME}Config.h.in ${CMAKE_CURRENT_BINARY_DIR}/${TARGET_NAME}Config.h @ONLY)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/${TARGET_NAME}.pc.in  ${CMAKE_CURRENT_BINARY_DIR}/${TARGET_NAME}Config.pc @ONLY)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/${TARGET_NAME}Config.cmake.in ${CMAKE_CURRENT_BINARY_DIR}/${TARGET_NAME}Config.cmake @ONLY)

install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${TARGET_NAME}Config.pc DESTINATION ${INSTALL_PKGCONFIG_DIR})
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${TARGET_NAME}Config.h DESTINATION ${INSTALL_INC_DIR})
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${TARGET_NAME}Config.cmake DESTINATION ${INSTALL_LIB_DIR}/cmake/${TARGET_NAME})

# uninstall target
if(NOT TARGET uninstall)
    configure_file(
    "${CMAKE_CURRENT_SOURCE_DIR}/cmake_uninstall.cmake.in"
    "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake"
    IMMEDIATE @ONLY)

    add_custom_target(uninstall
    COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake)
endif()

